package hotel.meituan.oversea.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import hotel.base.oversea.dto.meituan.MtHotelDetailDto;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.meituan.request.hotel.MtHotelDetailForFpReqVO;
import hotel.base.oversea.vo.meituan.request.room.CompareRoomForFpVO;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRoomForFpVO;
import hotel.base.oversea.vo.meituan.response.hotel.MtHotelDetailForFpRspVO;
import hotel.meituan.oversea.api.entity.MtHotelDetail;
import hotel.meituan.oversea.biz.mapper.MtHotelDetailMapper;
import hotel.meituan.oversea.biz.mapper.MtHotelRoomMapper;
import hotel.meituan.oversea.biz.service.MtHotelDetailService;
import hotel.meituan.oversea.biz.service.MtHotelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MtHotelDetailServiceImpl extends BaseServiceImpl implements MtHotelDetailService {
    @Autowired
    private MtHotelDetailMapper mtHotelDetailMapper;
    @Autowired
    private MtHotelService mtHotelService;
    @Autowired
    private MtHotelRoomMapper mtHotelRoomMapper;


    @Override
    public Result<Object> findAllHotelDetailPutRedis() {
        Example example = new Example(MtHotelDetail.class);
        /*example.createCriteria().andCondition("switch_hotel_id=", masterHotelId)
                .andCondition("is_compare = 1");*/
        List<MtHotelDetail> list = mtHotelDetailMapper.selectByExample(example);
        Map redisAppendMap = new HashMap();
        for (MtHotelDetail entity : list) {
            MtHotelDetailDto mtHotelDetailDto = new MtHotelDetailDto();
            mtHotelDetailDto.setMtHotelId(entity.getMtHotelId());
            mtHotelDetailDto.setMtHotelName(entity.getMtHotelName());
            mtHotelDetailDto.setMtHotelLongitude(entity.getMtHotelLongitude());
            mtHotelDetailDto.setMtHotelLatitude(entity.getMtHotelLatitude());
            mtHotelDetailDto.setMtHotelCityName(entity.getMtHotelCityName());
            mtHotelDetailDto.setMtLocationName(entity.getMtLocationName());
            mtHotelDetailDto.setMtCityLocationId(entity.getMtCityLocationId());
            mtHotelDetailDto.setMtAvgScore(entity.getMtAvgScore());
            mtHotelDetailDto.setMtPhone(entity.getMtPhone());
            mtHotelDetailDto.setMtOpeninfo(entity.getMtOpeninfo());
            mtHotelDetailDto.setMtCloseStatus(entity.getMtCloseStatus());
            mtHotelDetailDto.setMtFrontImage(entity.getMtFrontImage());
            mtHotelDetailDto.setMtBrandId(entity.getMtBrandId());
            mtHotelDetailDto.setMtBrandName(entity.getMtBrandName());
            mtHotelDetailDto.setMtOpenDate(entity.getMtOpenDate());
            mtHotelDetailDto.setMtDecorationDate(entity.getMtDecorationDate());
            mtHotelDetailDto.setMtRoomNum(entity.getMtRoomNum());
            mtHotelDetailDto.setMtFloorNum(entity.getMtFloorNum());
            mtHotelDetailDto.setMtHotelStar(entity.getMtHotelStar());
            mtHotelDetailDto.setMtHotelCheckinTimeBegin(entity.getMtHotelCheckinTimeBegin());
            mtHotelDetailDto.setMtHotelCheckinTimeEnd(entity.getMtHotelCheckinTimeEnd());
            mtHotelDetailDto.setMtStatusType(entity.getMtStatusType());
            redisAppendMap.put(mtHotelDetailDto.getMtHotelId(), mtHotelDetailDto);

        }
        mtHotelService.redisSaveUpdateMtHotelInfos(redisAppendMap);
        return new Result<>();
    }

    /**
     * <H1>获取美团酒店及房型相关信息</H1>
     *
     * @param mtHotelDetailForFpReqVO 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/2 17:27
     */
    @Override
    public Result<List<MtHotelDetailForFpRspVO>> getHotelInfoForFp(MtHotelDetailForFpReqVO mtHotelDetailForFpReqVO) {
        //PageHelper.startPage(mtHotelDetailForFpReqVO.getPageCode(), mtHotelDetailForFpReqVO.getPageSize());
        List<CompareRoomForFpVO> roomInfoList = mtHotelDetailForFpReqVO.getRoomInfoList();
        //美团酒店ID
        List<String> mtHotelIdList = roomInfoList.stream().distinct().map(CompareRoomForFpVO::getMtHotelId).collect(Collectors.toList());
        //分页查询获取美团酒店
        mtHotelDetailForFpReqVO.setMtHotelIds(mtHotelIdList);
        List<MtHotelDetailForFpRspVO> hotelList = mtHotelDetailMapper.getHotelInfoForFp(mtHotelDetailForFpReqVO);
        //美团房型ID
        List<String> mtRoomIdList = roomInfoList.stream().map(CompareRoomForFpVO::getMtRoomId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(mtRoomIdList)) {
            //房型及床型信息
            List<MtHotelRoomForFpVO> roomList = mtHotelRoomMapper.getHotelRoomInfo(mtRoomIdList);
            //组装数据
            for (MtHotelDetailForFpRspVO detailForFpRspVO : hotelList) {
                List<MtHotelRoomForFpVO> list = new ArrayList<>();
                for (MtHotelRoomForFpVO mtHotelRoomForFpVO : roomList) {
                    if (detailForFpRspVO.getMtHotelId().equals(mtHotelRoomForFpVO.getMtHotelId())) {
                        list.add(mtHotelRoomForFpVO);
                    }
                }
                detailForFpRspVO.setRoomInfoList(list);
            }
        }
        //PageInfo<MtHotelDetailForFpRspVO> pageInfo = new PageInfo<>(hotelList);
        return new Result<>(hotelList);
    }
}
